package com.quotes.ui;

import java.awt.event.*;
import javax.swing.*;
import org.apache.log4j.Logger;

import com.quotes.Parameters;
import com.quotes.datamodell.DataRepository;

public class MainWindow extends JFrame 
{
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(getClass().toString());
	private MainWindowEventListener eventListener;
	private DataRepository repository = null;
	private Parameters parameters = null;

	public MainWindow(DataRepository repository, Parameters parameters) {
		super("StockTradeAnalysis");
		this.setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
        this.eventListener = new MainWindowEventListener(this);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.repository = repository;
		this.repository.addDataChangedEventListener(eventListener);
		this.parameters = parameters;
		
        setJMenuBar(createMenu());
        setContentPane(createTabbedPane());
		
		logger.debug("MainWindow created!");
	}
	
	public DataRepository getRepository() {
		return repository;
	}
	
	private JTabbedPane createTabbedPane() {
		JTabbedPane tabbedPane = new JTabbedPane();
		
		JPanel controlPanel = new SymbolsPanel(repository);
		tabbedPane.addTab("Symbols", controlPanel);
		
		JPanel analysisPanel = new AnalysisPanel(repository, parameters);
		tabbedPane.addTab("Analysis", analysisPanel);
		
		JPanel simulationPanel = new SimulationPanel(repository, parameters);
		tabbedPane.addTab("Simulation", simulationPanel);
	
		JPanel loggingPanel = new LoggingPanel();
		tabbedPane.addTab("Logging", loggingPanel);
		Logger.getRootLogger().addAppender(new LoggingPanelAppender((LoggingTarget)loggingPanel));

		return tabbedPane;
	}
	
	private JMenuBar createMenu() {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu menu = new JMenu("File");
		menu.setMnemonic(KeyEvent.VK_F);
		menuBar.add(menu);

		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.setMnemonic(KeyEvent.VK_E);
		exitItem.addActionListener(this.eventListener);
		menu.add(exitItem);

		return menuBar;
	}
}
